#include "loger.h"
#include <QTextStream>

std::mutex mutex;

ConsoleLoger::ConsoleLoger(int _minLvl)
{
    minLvl=_minLvl;
}
void ConsoleLoger::log(int lvl, const std::string &messege)
{
   mutex.lock();
   std::string type;
   if (lvl>minLvl)
       return;
   switch (lvl) {
   case 3:
       type= "INFO";
       break;
   case 2:
       type = "WARNING";
       break;
   case 1:
       type= "Error";
       break;
   }
   std::cout << type <<": " << messege<<std::endl;
   mutex.unlock();
}

FileLoger::FileLoger(int _minLvl, QString str)
{
    minLvl=_minLvl;
    pyth=str;
}
void FileLoger::log(int lvl, const std::string &massage)
{
    mutex.lock();
    QString str = QString::fromUtf8(massage.c_str());
    QString type;
    QFile file(pyth);
    file.open(QIODevice::WriteOnly);
    QTextStream stream(&file);
    if (lvl>minLvl)
        return;
    switch (lvl) {
    case 3:
        type= "INFO";
        break;
    case 2:
        type = "WARNING";
        break;
    case 1:
        type= "Error";
        break;
    }
    stream << str << ": " << type <<endl;
    mutex.unlock();
    file.close();
}
